<?php 
try {
	include_once('db/constants.php');
	include_once('db/update_user_points_db.php');
	include_once('db/contents_ranking_db.php');
	include_once('db/general_functions_db.php');
	include_once('db/error_log_db.php');
	include_once('facebook_login.php');
	
	$current_file = "uploadContent";
	include_once("users_flow_logger.php");

	define("CONTENTS_SHOWN_PER_PAGE", "8");
	define("AMOUNT_BEST_CONTENT_TO_SHOW", 24);
	define("MAX_CONTENT_HEIGTH", "38");
	define("MAX_PAGES_OPTIONS", "5");

	
	$contents_json = get_best_content_db(AMOUNT_BEST_CONTENT_TO_SHOW, $user_profile['id']);
	$contents = json_decode( $contents_json );
	
	$number_pages = ceil( count($contents) / CONTENTS_SHOWN_PER_PAGE );
	
	$category_id = $_REQUEST['category_id'];

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow-x: hidden">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo HTML_TITLE;?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
	<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
	
	<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
	<script src="js/spin.js" type="text/javascript"></script>
	<script src="js/wrap_content.js?<?php echo REVISION;?>" type="text/javascript"></script>
	
	<script>
		var CONTENTS_SHOWN_PER_PAGE = <?php echo CONTENTS_SHOWN_PER_PAGE; ?>;
		var MAX_CONTENT_HEIGTH = <?php echo MAX_CONTENT_HEIGTH; ?>;
		var contents = JSON.parse( '<?php echo $contents_json; ?>' );
		var currentPage = 1;
		var numberPages = <?php echo $number_pages; ?>;

		function nextPage() {
			if (Math.ceil(contents.length/CONTENTS_SHOWN_PER_PAGE) > currentPage ) {
				currentPage++;
				
				showPageContent();
				showPageButtons();
			} 			
		}
		
		function prevPage() {
			if (currentPage > 1) {
				currentPage--;
				
				showPageContent();
				showPageButtons();
			}
		}
		
		function goToPage(number) {
			if (Math.ceil(contents.length/CONTENTS_SHOWN_PER_PAGE) >= number ) {
				currentPage = number;
				
				showPageContent();
				showPageButtons();
			} 
		}
		
		function showPageContent() {
			var from = (currentPage - 1)*CONTENTS_SHOWN_PER_PAGE;
			var to = (currentPage*CONTENTS_SHOWN_PER_PAGE < contents.length) ? currentPage*CONTENTS_SHOWN_PER_PAGE : contents.length;
			
			for (var i = from; i < to; i++) {
				showContentAt(i - (currentPage - 1)*CONTENTS_SHOWN_PER_PAGE, contents[i]);
			}
			
			for (var i = to; i < currentPage*CONTENTS_SHOWN_PER_PAGE; i++) {
				var index = i - (currentPage - 1)*CONTENTS_SHOWN_PER_PAGE;
				
				document.getElementById('content-div-' + index).style.display = 'none';
				document.getElementById('hr-' + index).style.display = 'none';
			}
		}
		
		function showContentAt(index, content) {
			var POINTS = "<?php echo POINTS;?>";

			document.getElementById('content-div-' + index).style.display = '';
			document.getElementById('hr-' + index).style.display = '';
			
			document.getElementById('rank-td-' + index).innerHTML = content.rank + '.';
			document.getElementById('content-td-' + index).innerHTML = content.content;
			document.getElementById('content-td-' + index).href = '<?php echo CONTENTS_URL;?>?content_id=' + content.content_id;
			document.getElementById('likes-td-' + index).innerHTML = content.likes;
			document.getElementById('dislikes-td-' + index).innerHTML = content.dislikes;
			document.getElementById('fraction-likes-td-' + index).innerHTML = content.fraction_likes*100 + '%';
			document.getElementById('points-td-' + index).innerHTML = parseFloat(content.points).toFixed( <?php echo POINTS_DECIMALS; ?> ) + " " + POINTS;
			document.getElementById('author-name-' + index).innerHTML = content.first_name + " " + content.last_name;
			document.getElementById('share-button-' + index).onclick = function() {	shareContent(content.content_id); }
			
			wrapContentToHeight('content-td-' + index, MAX_CONTENT_HEIGTH);

			if ( content.fraction_likes > 0.5 )
				document.getElementById('fraction-likes-td-' + index).className = 'points-good';
			else 
				document.getElementById('fraction-likes-td-' + index).className = 'points-bad';
		}
		
		function showPageButtons() {
			var MAX_PAGES_OPTIONS = <?php echo MAX_PAGES_OPTIONS; ?>;
			var pagesInnerHTML = '';
			
			if (MAX_PAGES_OPTIONS >= numberPages) {
				for (var i = 1; i <= numberPages; i++) {
					if (i == currentPage)
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number-selected" href="#" onclick="goToPage(' + i+ ')" >';
					else
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number" href="#" onclick="goToPage(' + i + ')" >';
						
					pagesInnerHTML += i;
						
					pagesInnerHTML += '</a>';
				}
			} else {
				var pagesEachSide = Math.floor(MAX_PAGES_OPTIONS/2);
				
				if (currentPage + pagesEachSide <= numberPages) {
					var startingPage = (pagesEachSide >= currentPage) ? 1 : currentPage - pagesEachSide;
					var endPage =  startingPage + MAX_PAGES_OPTIONS;
				} else {
					var startingPage = numberPages - MAX_PAGES_OPTIONS + 1;
					var endPage =  numberPages + 1;
				}
				
				for (var i = startingPage; i < endPage; i++) {
					if (i == currentPage)
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number-selected" href="#" onclick="goToPage(' + i + ')" >';
					else
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number" href="#" onclick="goToPage(' + i + ')" >';
						
					pagesInnerHTML += i;
						
					pagesInnerHTML += '</a>';
				}
			}

			if (document.getElementById('pages'))
				document.getElementById('pages').innerHTML = pagesInnerHTML;
		}
		
	</script>
</head>
<body>

	<div id="fb-root"></div>
	<script>(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/es_LA/all.js";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));</script>


    <script>		
	window.fbAsyncInit = function() {
	FB.init({
	  appId : '<?php echo APP_ID;?>',
	  xfbml : true,
	  version : 'v2.0'
	});
	};
	(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/es_LA/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
      
	function postFeed(content_id) {
		FB.ui({
		method: 'share',
		href: '<?php echo APP_CANVAS.CONTENTS_URL; ?>?content_id=' + content_id,
		}, function(response){});
	}
	
	function shareContent(content_id) {
		$.post("db/log.php?user_id=<?php echo $user_profile['id']; ?>", {
			}, function(response){ });
		
		postFeed(content_id);		
	}
      
    </script>

<?php 
$headerFirstOption = SEE_MY_PUBLICATIONS;
$headerSecondOption = CATEGORIES;
$headerThirdOption = PUBLISH;

$headerFirstUrl = PUBLISH_CONTENT."?category_id=".$category_id."&coming_from=".$current_file;
$headerSecondUrl = CATEGORIES_URL."?category_id=".$category_id."&coming_from=".$current_file;
$headerThirdUrl = UPLOAD_CONTENT."?category_id=".$category_id."&coming_from=".$current_file;

$headerFirstClass = "button";
$headerSecondClass = "button-highlighted-green";
$headerThirdClass = "button-highlighted-red";

include_once("header_redirects.php"); 
include_once("header.php"); 
?>

<!-- Main -->
<div id="main">


	<div class="published-content-main-div">
		
		<!-- Cols -->
		<div style="height: 660px;">
			<div class="cl">&nbsp;</div>
			
			<!-- Col -->
			<div class="col" align="center" style="float: none; ">
				<h3><? echo BEST_CONTENTS;?></h3>

				<hr/>
				
				<?php
				for ($i = 0; $i < CONTENTS_SHOWN_PER_PAGE; $i++) {
									
					echo '<div id="content-div-'.$i.'">';
						echo '<table class="best-content-page-table" border="0px" cellpadding="0px" cellspacing="0px">';
							echo '<tr>';
								echo '<td id="rank-td-'.$i.'" valign="top" class="ranking-number" ></td>';
								echo '<td rowspan="3" valign="middle" class="published-content-content-td" > <a id="content-td-'.$i.'" class="no-link" ></a> </td>';
								echo '<td > <img src="css/images/happySmall.png" /> </td>';
								echo '<td id="likes-td-'.$i.'" class="points-good" width="70px" ></td>';
								echo '<td id="fraction-likes-td-'.$i.'" class="points-good" style="padding-left: 0px;"></td>';
								echo '<td rowspan="4" style="padding-left: 35px;"> <a id="share-button-'.$i.'" class="button" href="#">'.SHARE.'</a> </td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td></td>';
								echo '<td style="padding: 3px;" ></td>';
								echo '<td></td>';
								echo '<td></td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td height="6px" > </td>';
								echo '<td rowspan="2"> <img src="css/images/sadSmall.png" />  </td>';
								echo '<td rowspan="2" id="dislikes-td-'.$i.'" class="points-bad"></td>';
								echo '<td rowspan="2" id="points-td-'.$i.'" class="points"></td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td></td>';
								echo '<td id="author-name-'.$i.'" height="12px" align="right" style="padding-right:35px;" class="author-name"></td>';
							echo '</tr>';
						echo '</table>';
					echo '</div>';
				
					echo '<hr style="align: center;" id="hr-'.$i.'"/>';
				}
				?>
				
				<?php
				if ($number_pages > 1) {
					echo '<table border="0px" class="page-navigation-buttons">';
					echo '<tr>';
						echo '<td> <a class="previous-page-button" onclick="prevPage()" href="#"></a> </td>';
						echo '<td id="pages" class="pages"> ';
						echo '</td>';
						echo '<td> <a class="next-page-button" onclick="nextPage()" href="#"></a> </td>';
					echo '</tr>';
					echo '</table>';
				} ?>
				
			</div>
			<!-- End Col -->
					
		</div>
	</div>
</div>
<!-- End Main -->

<script>
	showPageContent();

	var spinnerTarget = document.getElementById('main');
	var spinner = new Spinner().spin(spinnerTarget);
	spinner.stop();
	
	showPageButtons();
</script>

</body>
</html>
